<template>
  <div>
    <el-card>
      <div slot="header">
        <span>就诊信息</span>
      </div>
      <el-descriptions title="" :column="4" border style="margin: 10px">
        <el-descriptions-item>
          <template slot="label">姓名</template>
          {{ clinicForm.patientName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">年龄</template>
          {{ clinicForm.patientAge }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">性别</template>
          {{ clinicForm.patientGender==0?'男': clinicForm.patientGender==1? '女':'未知' }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">患者来源</template>
          {{ clinicForm.source == '0' ? "住院" : "门诊"}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">病种</template>
          {{ clinicForm.disease }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">科室</template>
          {{ clinicForm.department }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">病区</template>
          {{ clinicForm.ward }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">病床</template>
          {{ clinicForm.bedNum }}
        </el-descriptions-item>
        <el-descriptions-item :span="4">
          <template slot="label">诊断</template>
          {{ clinicForm.preDiagnose }}
        </el-descriptions-item>
        <el-descriptions-item :span="4">
          <template slot="label">病情摘要</template>
          {{ clinicForm.illnessSummary }}
        </el-descriptions-item>
        <el-descriptions-item :span="4">
          <template slot="label">既往史</template>
          {{ clinicForm.preHistory }}
        </el-descriptions-item>
        <el-descriptions-item :span="4">
          <template slot="label">家族史</template>
          {{ clinicForm.familyHistory }}
        </el-descriptions-item>
      </el-descriptions>
    </el-card>

    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-card>
        <div slot="header">
          <span>任务分配</span>
        </div>
        <div>
          <el-row type="flex" justify="center" :gutter="0">
            <el-col :span="2">
              <el-button style="background:#fa964a;color: #ffffff;" @click="selectDoctor">选择评估医生</el-button>
            </el-col>
            <el-col :span="9">
              <el-form-item prop="doctorId">
                <el-input v-model="doctorName" style="width: 40%;" readonly placeholder="请选择" />
              </el-form-item>
            </el-col>
            <el-col :span="2">
              <el-button style="background:#fa964a;color: #ffffff;" @click="selectAssess">选择评定表</el-button>
            </el-col>
            <el-col :span="9">
              <el-form-item prop="assessId">
                <el-input v-model="assessName" style="width: 50%" readonly placeholder="请选择" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <div style="margin: 10px auto; text-align: center">
          <el-button type="primary" @click="submitForm">提 交</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-card>

    </el-form>

    <el-dialog :title="title" :visible.sync="visdoctor" width="700px" append-to-body>
      <div class="dialog-style">
        <el-table v-loading="loading" @row-dblclick="dblclickSelectDoc" :data="userOptions.filter(data => !searchDoctor || data.nickName.toLowerCase().includes(searchDoctor.toLowerCase()))">
          <el-table-column label="序号" type="index" width="50"/>
          <el-table-column label="医生名称" align="center" key="nickName" prop="nickName"/>
          <el-table-column align="right">
            <template slot="header" slot-scope="scope">
              <el-input
                v-model="searchDoctor"
                size="mini"
                placeholder="输入关键字搜索"/>
            </template>
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleSelect(scope.row)"
              >分配
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close1">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="visassess" width="700px" append-to-body>
      <div class="dialog-style">
        <el-table v-loading="loading" @row-dblclick="dblclickSelectAss" :data="assessList.filter(data => !searchAssess || data.assessName.toLowerCase().includes(searchAssess.toLowerCase()))">
          <el-table-column label="评定表编号" align="center" type="index" width="100"/>
          <el-table-column label="评定表名称" align="center" prop="assessName"/>
          <!--          <el-table-column label="评定表配置" align="center" prop="assessConfig" width="600"/>-->
          <el-table-column label="类别" align="center" prop="cateName"></el-table-column>
          <el-table-column align="right">
            <template slot="header" slot-scope="scope">
              <el-input
                v-model="searchAssess"
                size="mini"
                placeholder="输入关键字搜索"/>
            </template>
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleAssign(scope.row)"
              >分配
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {delAssess, getAssess, listAssess} from "@/api/gaoxin/assess";
import {addTask, delTask, getTask, listTaskalive} from "@/api/gaoxin/taskdata";
import {getAuthAssess, getUser, getUserAll} from "@/api/system/user";
import {getUserProfile} from "@/api/system/user";
import {getClinic} from "@/api/gaoxin/clinic";
import {listCategory} from "@/api/system/category";
import {addMessage} from "@/api/gaoxin/message";

export default {
  name: "Task",
  dicts: ['department','disease','ward','marital_status','sys_user_sex','patient_source'],
  data() {
    return {
      //是否选择医生
      hasDocter: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      assessName: "",
      // 表单参数
      clinicId: '',
      clinicForm: {},
      categoryList:[],
      form: {
        clinicId: '',
        masterId: "",
        doctorId: "",
        assessId: "",
        status: "1",
        charge:true
      },
      //患者数据
      patientOptions: [],
      userOptions: [],
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户总条数
      utotal: 0,
      // 评定总条数
      atotal: 0,
      // assess数据
      assesses: [],
      // assess表格数据
      assessList: [],
      tempList: [],
      // 显示搜索条件
      showSearch: true,
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assessName: null,
        pdType: null,
        userId: '',
      },

      // 表单校验
      rules: {
        masterId: [{
          required: true,
          message: "主治医生不能为空",
          trigger: ['blur']
        }
        ],
        doctorId: [{
          required: true,
          message: "评估医生不能为空",
          trigger: ['blur']
        }],
        assessId: [
          {
            required: true,
            message: "评定表选择不能为空",
            trigger: ['blur']
          }
        ],
      },
      //分配任务医生姓名
      masterName: "",

      //评估医生
      doctorName: null,
      visdoctor: false,
      doctorList: [],
      searchDoctor: '',
      //评估表
      visassess: false,
      searchAssess: '',


    };

  },
  created() {

    const path = this.$route.path;
    const clinicId = Number(path.split('/').slice(-1));
    this.clinicId = clinicId;
    this.form.clinicId = clinicId;

    getUserProfile().then(res => {
      this.form.masterId = res.data.userId;
      this.masterName = res.data.nickName;
    })
    this.getInfo();
    this.getListCategory();
  },
  watch: {
    "form.doctorId": {
      handler(newVal, oldVal) {
        this.assesses = null;
        let tempList = [];
        this.assessList = null;
        getAuthAssess(newVal).then(response => {
          this.assesses = response.assesses;
          for (let i = 0; i < this.assesses.length; i++) {
            if (this.assesses[i].flag) {
              tempList.push(this.assesses[i]);
            }
          }
          this.assessList = tempList;
        });
      }
    }
  },
  methods: {
    selectDoctor() {
      this.visdoctor = true;
      getUserAll().then(response => {
        this.userOptions = response.rows;
        this.utotal = response.total
      });
    },
    handleSelect(row) {
      this.form.assessId = null;
      this.assessName = null
      this.form.doctorId = row.userId;
      this.doctorName = row.nickName
      this.close1();
    },

    selectAssess() {
      if (this.doctorName == null) {
        this.$modal.msgWarning("请先选择评估医生");
        return;
      } else {
        this.visassess = true;
      }

    },
    topinggu() {
      this.$modal.confirm('是否确认评估医生为本人，且现在就开始评估').then(() => {
        getUserProfile().then(res => {
          // console.log(this.form)
          this.form.doctorId = res.data.userId;
          this.$refs["form"].validate(valid => {
            if (valid) {
              this.form.charge=this.form.charge?1:0;
              addTask(this.form).then(() => {
                // console.log(this.form)
                listTaskalive(this.form).then(res => {
                  var tasklist = res.rows;
                  var task = tasklist[tasklist.length - 1]
                  this.$router.push({
                    name: 'dataAdd', params: {
                      taskdata: task,
                      assid: this.form.assessId
                    }
                  });
                })
              });
            }
          });
        })
      })
    },

    getInfo() {
      let clinicId = this.clinicId;
      getClinic(clinicId).then(response => {
        this.clinicForm = response.data;
        // console.log(this.clinicForm)
      })
    },
    handleAssign(row) {
      this.form.assessId = row.assessId;
      this.form.assessConfig = row.assessConfig;
      this.assessName = row.assessName;
      this.close2();

    },

    selectPatientInfo() {
      // console.log(this.patientOptions);
      let item = this.patientOptions.find(item => item.patientId === this.form.patientId)
      this.form.ward = item.ward;
      this.form.department = item.department;
    },


    getListCategory() {
      this.loading = true;
      listCategory().then(response => {
        this.categoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.$tab.closePage()
      this.$router.go(-1)
    },
    // 关闭页面
    close1() {
      this.visdoctor = false;
      this.userOptions = [];
    },
    close2() {
      this.visassess = false;
      this.userOptions = [];
    },
    // 表单重置
    reset() {
      this.form = {
        assessId: null,
        assessName: null,
        assessConfig: null,
        remark: null,
        charge:null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.assessId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加assess";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const assessId = row.assessId || this.ids
      getAssess(assessId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改assess";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.charge = this.form.charge?1:0;
          addTask(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.$tab.closeOpenPage({path: "/workflow/execute/index"})
          });
        }
      });
    },
    dblclickSelectDoc(item) {
      this.handleSelect(item)
    },
    dblclickSelectAss(item) {
      this.handleAssign(item);
    }
  }
};
</script>

<style scoped>

.el-card {
  margin: 20px;
  /*min-height: 200px;*/
}
.el-checkbox{
  margin-top: 5px;
  zoom: 120%;
}
.dialog-style{
  height: 60vh;
  overflow-x: hidden;
  overflow-y: auto;
}

</style>
